package com.squirrel.michale;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/21 7:15 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2593 {

    public long findScore(int[] nums) {
        int size = nums.length;
        PriorityQueue<Element> heap = new PriorityQueue<>(new Comparator<Element>() {
            @Override
            public int compare(Element o1, Element o2) {
                return o1.value == o2.value ? o1.index - o2.index : o1.value - o2.value;
            }
        });
        for (int i = 0; i < size; i++) {
            heap.add(new Element(nums[i],i));
        }

        boolean[] visited = new boolean[size];
        long result = 0;
        while (!heap.isEmpty()) {
            Element element = heap.poll();
            int index = element.index;
            if (!visited[index]) {
                result +=element.value;
                visited[index] = true;
                if (index < size-1) {
                    visited[index + 1] = true;
                }
                if (index > 0) {
                    visited[index - 1] = true;
                }
            }
        }
        return result;
    }

    class Element{
        int value;
        int index;

        public Element(int value, int index) {
            this.value = value;
            this.index = index;
        }
    }

    public static void main(String[] args) {
        LeetCode2593 leetCode2593 = new LeetCode2593();

        int[] nums = new int[]{2, 1, 3, 4, 5, 2};
        System.out.println(leetCode2593.findScore(nums));
    }
}
